package com.liaowei.novel.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.liaowei.novel.R;
import com.liaowei.novel.fragment.BookshelfFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class HistoryActivity extends AppCompatActivity {

    private SQLiteDatabase db;
    private ListView history_lv;
    private String[] from = {"id", "coverImage", "name","categoryName","author","description"};
    private int[] to = {R.id.history_id,R.id.history_cover_image, R.id.history_name,R.id.history_category,R.id.history_author,R.id.history_description};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_history);
        setTitle("浏览记录");
        init();
        SimpleAdapter adapter = new SimpleAdapter(HistoryActivity.this, getData(), R.layout.history_item, from, to);
        history_lv.setAdapter(adapter);

    }



    public List<Map<String, Object>> getData(){
        String sql = "select n.id,n.name,n.description,n.coverImage,u.nickname as author,g.name as category_name\n" +
                "from novel n\n" +
                "join history h on h.novel_id = n.id and h.user_id = 8\n" +
                "join category g on g.id = n.category_id\n" +
                "join user u on u.id = n.user_id\n" +
                "order by h.create_time desc";
        Cursor cursor = db.rawQuery(sql, null);
        List<Map<String, Object>> list = new ArrayList<>();
        while (cursor.moveToNext()) {
            int id = cursor.getInt(cursor.getColumnIndex("id"));
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String coverImage = cursor.getString(cursor.getColumnIndex("coverImage"));
            String author = cursor.getString(cursor.getColumnIndex("author"));
            String categoryName = cursor.getString(cursor.getColumnIndex("category_name"));
            String description = cursor.getString(cursor.getColumnIndex("description"));

            HashMap<String, Object> map = new HashMap<>();
            map.put("id", id);
            map.put("coverImage", coverImage);
            map.put("name", name);
            map.put("author", author);
            map.put("categoryName", categoryName);
            if (description.length() > 45){
                description = description.substring(0,45) + "...";
            }
            map.put("description", description);

            list.add(map);
        }
        return list;
    }


    class SimpleAdapter extends android.widget.SimpleAdapter {
        public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) {
            super(context, data, resource, from, to);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View v = super.getView(position, convertView, parent);
            Map<String, Object> item = (Map<String, Object>) getItem(position);
            String coverImage = (String) item.get("coverImage");
            ImageView iv = v.findViewById(R.id.history_cover_image);
            Glide.with(v).load(coverImage).into(iv);

            LinearLayout line = v.findViewById(R.id.history_item);
            line.setOnClickListener(new View.OnClickListener(){
                @Override
                public void onClick(View v) {
                    TextView tv_id = v.findViewById(R.id.history_id);
                    String strId = tv_id.getText().toString();
                    int id = Integer.parseInt(strId);
                    Intent intent = new Intent(HistoryActivity.this, NovelDetailActivity.class);
                    intent.putExtra("id",id);
                    startActivity(intent);
                }
            });

            return v;
        }
    }


    // 初始化组件
    public void init(){
        this.db = SQLiteDatabase.openOrCreateDatabase("/data/data/com.liaowei.novel/databases/novel.db", null);
        this.history_lv = findViewById(R.id.history_lv);
    }
}